import { sql } from "#/utils";
import type { Test } from "#/types";

export const queryList = (type: number) => sql`
    SELECT * FROM t_name WHERE TYPE = ${type} ORDER BY \`rank\` ASC`;

export async function getRank() {
  const manageList = await sql`
    SELECT * FROM t_name ORDER BY \`rank\` DESC LIMIT 1`;
  return manageList.length > 0 ? manageList[0].rank + 1 : 1;
}

export const insert = async (data: Test) => {
  return sql`
    INSERT INTO t_name 
    (id, name, color , source, type,createTime,\`rank\`) 
    VALUES 
    (
      ${data.id},
      ${data.name},
      ${data.color},
      ${data.source},
      ${data.type},
      ${data.createTime},
      ${data.rank},
    )`;
};
export const deleteById = (id: string) =>
  sql` DELETE FROM tableName  WHERE id =${id} `;

export const update = async (data: Test) => {};

export default {
  queryList,
  insert,
  deleteById,
  update,
  getRank,
  queryById: (id: number) => sql`SELECT * FROM tableName  WHERE id =${id} `,
};
